package org.budo.dubbo.http.client.java.callback;

import org.budo.dubbo.http.client.java.RequestContext;
import org.budo.dubbo.http.client.java.exception.ApiErrorException;

/**
 * 合并两个回调
 * 
 * @author lmw
 */
public class MergedCallback implements ApiCallback {
    private AbstractCallback callback;

    private AbstractCallback callbackDefault;

    /**
     * 优先用第一个
     */
    public MergedCallback(AbstractCallback callback, AbstractCallback callbackDefault) {
        this.callback = callback;
        this.callbackDefault = callbackDefault;
    }

    @Override
    public void success(Object result, RequestContext request) {
        if (this.callback instanceof ApiSuccess) {
            ((ApiSuccess) this.callback).success(result, request);
        } else {
            ((ApiSuccess) this.callbackDefault).success(result, request);
        }
    }

    @Override
    public void fail(ApiErrorException error, RequestContext request) {
        if (this.callback instanceof ApiFail) {
            ((ApiFail) this.callback).fail(error, request);
        } else {
            ((ApiFail) this.callbackDefault).fail(error, request);
        }
    }
}